<?php
	header("Content-Type: text/html;charset=utf-8");
	include("function/_nosql.php"); //
	include("function/function.php");
	include("_include.user.judge.php");
	require_once "weipay/lib/WxPay.Api.php";
    require_once "weipay/WxPay.NativePay.php";
    require_once 'weipay/log.php';
$WIDout_trade_no = chkstr(trim($_REQUEST['WIDout_trade_no']));
$aid = chkstr(trim($_REQUEST['aid']));
$uid = $_SESSION['kboy_userid'];
	$time = date("Y");
	$order_num = getClassName("kboy_order","order_num","order_no='".$WIDout_trade_no."'");
	$moneys = getClassName("kboy_forum_active","active_all_money","id=$aid");
	$active_tian = getClassName("kboy_forum_active","active_tian","id=".$aid);
	$money = ($moneys*$order_num);
if(strstr($WIDout_trade_no,'_')){//申请成为cva
	 mysql_query("update  kboy_user  set user_trade_no='".$WIDout_trade_no."' where uid= '".$_SESSION['kboy_userid']."' ");
	 $money = 50;
	 $subject ='申请成为CVA';
}
else{//提交订单
	if(empty($aid)){
		echo erro("参数错误");
	}
	if(!is_numeric($aid)){
		echo erro("参数错误");
	}
	if(getNewsNums("kboy_forum_active","id=$aid") <1){
		echo erro("参数错误");
	}
	$order_num = getClassName("kboy_order","order_num","order_no='".$WIDout_trade_no."'");
	if(getClassName("kboy_order","order_isfull","order_no='".$WIDout_trade_no."'")  == 1 ){
		if(getClassName("kboy_user","user_style"," uid= '".$_SESSION['kboy_userid']."' ")  == 1){//为cva会员
			if(getClassName("kboy_jiangli","hdnian","uid=$uid and hd_jieshu=0") == $time and getClassName("kboy_jiangli","hd_yhui","uid=$uid and hd_jieshu=0") == 1){
					$leiji = getClassName("kboy_jiangli","hd_leiji","uid=$uid and hd_jieshu=0");
					$renshu = getClassName("kboy_jiangli","hdrenshu","uid=$uid and hd_jieshu=0");
					if($order_num == 1){
						if($active_tian < 3 and $renshu > 30){
						$money = ($money*0.90);
						$jl_shuom = "带入新会员".$renshu."人，优惠10%";
						}else if($active_tian < 3 and $leiji > 10 and $leiji < 31){
							$money = ($money*0.95);
							$jl_shuom = "累计参加".$leiji."次，优惠5%";
						}else if($active_tian < 3 and $leiji > 30){
							$money = ($money*0.90);
							$jl_shuom = "累计参加".$leiji."次，优惠10%";
						}
					}else if($order_num == 2 or $order_num == 3){
						if($active_tian < 3 and $renshu > 30){
							$benren = ($moneys*0.90);
							$money = ($order_num-1)*$moneys+$benren;
							$jl_shuom = "带入新会员".$renshu."人，和".($order_num-1)."位朋友，优惠10%";
						}else if($active_tian < 3 and $leiji > 10 and $leiji < 31){
							$benren = ($moneys*0.95);
							$money = ($order_num-1)*$moneys+$benren;
							$jl_shuom = "累计参加".$leiji."次，和".($order_num-1)."位朋友，优惠5%";
						}else if($active_tian < 3 and $leiji > 30){
							$benren = ($moneys*0.9);
							$money = ($order_num-1)*$moneys+$benren;
							$jl_shuom = "累计参加".$leiji."次，和".($order_num-1)."位朋友，优惠10%";
						}
					}else if($active_tian < 3 and $order_num-1 < 6 and  $order_num-1 > 2){
						$benren = ($moneys*0.70);
						$money = ($order_num-1)*$moneys+$benren;
							$jl_shuom = "参加本次活动，带新人".($order_num-1)."位，优惠30%";
					}else if($active_tian < 3 and $order_num-1 > 5){
						$benren = ($moneys*0.50);
						$money = ($order_num-1)*$moneys+$benren;
							$jl_shuom = "参加本次活动，带新人".($order_num-1)."位，优惠50%";
					}
					$jl_danhao= getClassName('kboy_jljilu','jl_danhao',"uid='$uid' and aid = '$aid'");
					sp($jl_danhao);
					if($jl_danhao == null){
						$mysql = new sqlstr("kboy_jljilu");
						$mysql -> set("uid",$uid);
						$mysql -> set("aid",$aid);
						$mysql -> set("jl_shuom",$jl_shuom);
						$mysql -> set("jl_danhao",$rUnique);
						$mysql -> set("jl_jine",$money);
						$mysql -> set("jl_time",date("Y-m-d H:i:s", time()));
						$news_sql = $mysql -> insertSql();
						$news_res = mysql_query($news_sql);
					}else{
						mysql_query("update kboy_jljilu jl_danhao='$WIDout_trade_no' where uid='$uid' and aid = '$aid'");
					}
									
									
					}else{
						$moneys = getClassName("kboy_forum_active","active_cva_money","id=$aid");
						$money = ($moneys*$order_num);
					}
		}
		else{
			$moneys = getClassName("kboy_forum_active","active_all_money","id=$aid");
			$money = ($moneys*$order_num);
		}
	}
	else{
	 $moneys = getClassName("kboy_forum_active","active_part_money","id=$aid");
	$money = ($moneys*$order_num);
	}
	$subject = getClassName("kboy_forum_active","active_title","id=$aid");
}
//模式一
/**
 * 流程：
 * 1、组装包含支付信息的url，生成二维码
 * 2、用户扫描二维码，进行支付
 * 3、确定支付之后，微信服务器会回调预先配置的回调地址，在【微信开放平台-微信支付-支付配置】中进行配置
 * 4、在接到回调通知之后，用户进行统一下单支付，并返回支付信息以完成支付（见：native_notify.php）
 * 5、支付完成之后，微信服务器会通知支付成功
 * 6、在支付成功通知中需要查单确认是否真正支付成功（见：notify.php）
 */
$notify = new NativePay();
//$url1 = $notify->GetPrePayUrl("123456789");

//模式二
/**
 * 流程：
 * 1、调用统一下单，取得code_url，生成二维码
 * 2、用户扫描二维码，进行支付
 * 3、支付完成之后，微信服务器会通知支付成功
 * 4、在支付成功通知中需要查单确认是否真正支付成功（见：notify.php）
 */
 //读取订单信息
 $money=  ($money*100);
$input = new WxPayUnifiedOrder();
$input->SetBody($subject);
$input->SetAttach($subject);
$input->SetOut_trade_no($WIDout_trade_no);
$input->SetTotal_fee($money);
$input->SetTime_start(date("YmdHis"));
$input->SetTime_expire(date("YmdHis", time() + 600));
$input->SetGoods_tag("中国徒步网");
$input->SetNotify_url("http://www.chinawalking.net.cn/weipay/notify.php");
$input->SetTrade_type("NATIVE");
$input->SetProduct_id($product_id);
$result = $notify->GetPayUrl($input);
$url2 = $result["code_url"];


?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>微信支付</title>
<meta name="Keywords" content="微信支付" />
<meta name="Description" content="微信支付" />
<link rel="stylesheet" href="css/common.css"/>
<link rel="stylesheet" href="css/sq.css"/>
<script type="text/javascript" src="js/laydate/laydate.js"></script>
</head>
<body>
    <!--top-bar begin-->
	<? include("_include.top.topbar.php") ?>
    <!--top-bar end-->
    <!--header begin-->
	<? include("_include.top.navbar.php") ?>
    <!--header end-->

   <div class="order-box">

       <div class="content">

           <div class="order-step"><i class="order-write"></i>微信支付</div>

			  <div style="margin:0 auto; text-align:center;">
				 <h1 style=" font-size:20px; color:#000; margin:10px 0px">微信扫描二维码支付！</h1>
				 <div style=" font-size:18px; margin-bottom:20px; margin:0 auto; text-align:center">
				 <span style="color:#000">订单号:<strong id="out_trade_no" style="color:#c90800"><?php echo trim($WIDout_trade_no);?></strong></span>
				 &nbsp;<span style="color:#000">金额为：<strong style=" color:#c90800"><?php echo ($money/100);?>元</strong></span>
				 <br/><br/>
			     <img alt="扫描二维码支付" src="http://www.chinawalking.net.cn/weipay/qrcode.php?data=<?php echo urlencode($url2);?>" style="width:200px;height:200px;"/>
				</div>
			 </div>

       </div>

   </div>
    <!--foot begin-->
	<? include("_include.bottom.footbar.php") ?>
    <!--foot begin-->
    <script type="text/javascript" src="js/jquery-1.10.2.min.js"></script>
    <script type="text/javascript" src="js/jquery.SuperSlide.2.1.1.js"></script>
    <script type="text/javascript" src="js/main.js"></script>
    <script type="text/javascript" src="js/sq.js"></script>
    <script type="text/javascript" src="js/cropbox.js"></script>
	<script type="text/javascript">
$(document).ready(function () {
	    setInterval("ajaxstatus()", 3000);    
	});
	function ajaxstatus() {
	    if ( $("#out_trade_no").html() != 0) {
	    	var out_no = $("#out_trade_no").html();
	        $.ajax({
	            url: "checkorder.php?action=weixinurl",
	            type: "post",
	            dataType:"html",
	            data: {WIDout_trade_no:out_no},
	            success: function (data) {
	                if (data==1) { //订单状态为1表示支付成功
	                   window.location.href = "user_order.php"; //页面跳转
	                }
	            },
	            error: function () {
	                // alert("请求订单状态出错"); 
	            }
	        });
	    }
	
	} 
</script>


</body>

</html>

